﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


namespace CodeStash.Common.DataAccess.AspMembership
{
    public class GetUserForRestService : IGetUserForRestService
    {
        public string GetUserFromEmail(string userId)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            string userName = string.Empty;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("sp_GetUserDataForUserId", connection))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserId", userId));

                    connection.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        if (reader.HasRows)
                        {
                            reader.Read();

                            userName = reader.GetString(1);
                        }
                    }
                }
            }

            return userName;
        }

        public List<RESTUser> GetUserForRest(string codeStashToken, string email, string password)
        {

            string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

            List<RESTUser> restUsers = new List<RESTUser>();

            using (SqlConnection memberShipDBConnection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("sp_GetUserForRest", memberShipDBConnection))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    if (!String.IsNullOrEmpty(codeStashToken))
                        cmd.Parameters.Add(new SqlParameter("@CodeStashToken", codeStashToken));
                    else
                        cmd.Parameters.Add(new SqlParameter("@CodeStashToken", DBNull.Value));

                    cmd.Parameters.Add(new SqlParameter("@Email", email));
                    cmd.Parameters.Add(new SqlParameter("@Password", password));

                    memberShipDBConnection.Open();
                    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                    
                    while (reader.Read())
                    {
                       
                        RESTUser user = new RESTUser(
                            reader.GetGuid(0),
                            reader.GetGuid(1),
                            reader.GetString(2),
                            reader.GetString(3),
                            reader.GetString(4),
                            reader.GetString(5));

                        restUsers.Add(user);
                    };
                    reader.Close();
                }
            }

            return restUsers;
        }
    }
}
